package com.fcy.teamwork.jmservice.event

import cn.jpush.im.android.api.content.*
import cn.jpush.im.android.api.content.EventNotificationContent.EventNotificationType
import cn.jpush.im.android.api.enums.ContentType
import cn.jpush.im.android.api.event.MessageEvent
import cn.jpush.im.android.api.model.GroupInfo
import cn.jpush.im.android.api.model.Message
import cn.jpush.im.android.api.model.UserInfo
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow


class ChatMsgEventReceiver(private val id: String) : BaseEventListener() {

    val pureTextDataSource = MutableSharedFlow<Message>(
        replay = 0,
        onBufferOverflow = BufferOverflow.SUSPEND,
        extraBufferCapacity = 1
    )

    //用户在线期间收到的消息都会以MessageEvent的方式上抛
    fun onEvent(event: MessageEvent) {
        val msg: Message = event.message
        when (msg.contentType) {
            ContentType.text -> {
                //处理文字消息
                val textContent = msg.content as TextContent
                textContent.text
                val targetId = when (val targetInfo = msg.targetInfo) {
                    is UserInfo -> {
                        targetInfo.userName
                    }
                    is GroupInfo -> {
                        targetInfo.groupID.toString()
                    }
                    else -> {
                        throw IllegalArgumentException("")
                    }
                }
                if (id == targetId) {
                    pureTextDataSource.tryEmit(msg)
                }
            }
            ContentType.image -> {
                //处理图片消息
                val imageContent = msg.getContent() as ImageContent
                imageContent.localPath //图片本地地址
                imageContent.localThumbnailPath //图片对应缩略图的本地地址
            }
            ContentType.voice -> {
                //处理语音消息
                val voiceContent = msg.getContent() as VoiceContent
                voiceContent.localPath //语音文件本地地址
                voiceContent.duration //语音文件时长
            }
            ContentType.custom -> {
                pureTextDataSource.tryEmit(msg)
                //处理自定义消息
                val customContent = msg.getContent() as CustomContent
                customContent.getNumberValue("custom_num") //获取自定义的值
                customContent.getBooleanValue("custom_boolean")
                customContent.getStringValue("custom_string")
            }
            ContentType.eventNotification -> {
                //处理事件提醒消息
                val eventNotificationContent = msg.getContent() as EventNotificationContent
                when (eventNotificationContent.eventNotificationType) {
                    EventNotificationType.group_member_added -> {}
                    EventNotificationType.group_member_removed -> {}
                    EventNotificationType.group_member_exit -> {}
                    EventNotificationType.group_info_updated -> {}
                    else -> {}
                }
            }
            ContentType.unknown -> {
                // 处理未知消息，未知消息的Content为PromptContent 默认提示文本为“当前版本不支持此类型消息，请更新sdk版本”，上层可选择不处理
                val promptContent = msg.getContent() as PromptContent
                promptContent.promptType //未知消息的type是unknown_msg_type
                promptContent.promptText //提示文本，“当前版本不支持此类型消息，请更新sdk版本”
            }
            else -> {}
        }
    }


}